﻿@model IEnumerable<eBusiness.Models.Product>
@functions {
    private string GetProductLink(int pageNumber = 1)
    {
        var link = "/Product/Index?";
        if (!string.IsNullOrEmpty(ViewBag.CurrentCategoryID))
        {
            link += "&categoryId=" + ViewBag.CurrentCategoryID;
        }
        if (!string.IsNullOrEmpty((ViewBag.CurrentIsHot).ToString()))
        {
            link += "&IsHot=" + ViewBag.CurrentIsHot;
        }
        if (!string.IsNullOrEmpty((ViewBag.CurrentIsDiscount).ToString()))
        {
            link += "&IsDiscount=" + ViewBag.CurrentIsDiscount;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentStatusID))
        {
            link += "&StatusID=" + ViewBag.CurrentStatusID;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftField))
        {
            link += "&softField=" + ViewBag.CurrentSoftField;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftDir))
        {
            link += "&softDir=" + ViewBag.CurrentSoftDir;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentViewType))
        {
            link += "&viewType=" + ViewBag.CurrentViewType;
        }
        if (!string.IsNullOrEmpty(pageNumber.ToString()))
        {
            link += "&pageNumber=" + pageNumber;
        }
        return link;
    }
}
@{
    ViewBag.Title = "Product By List";
    var categoryId = Session["Category"];
    var _unitOfWork = new eBusiness.App_DAL.UnitOfWork();
}
<script type="text/javascript">
    $(document).ready(function () {
        //        $("#ddlCategory").change(function (e) {
        //            window.location.href = '/Admin/Products?' + GetParams();
        //        });
        $("#SoftField").change(function (e) {
            window.location.href = '/Product?' + GetParams();
        });
        $("#SoftDir").change(function (e) {
            window.location.href = '/Product?' + GetParams();
        });
        $("#ddlViewType").change(function (e) {
            window.location.href = '/Product?' + GetParams();
        });
        $("#ddlStatusId").change(function (e) {
            window.location.href = '/Product?' + GetParams();
        });
        $("#cbxIsHot").change(function (e) {
            window.location.href = '/Product?' + GetParams();
        });
        $("#cbxIsDiscount").change(function (e) {
            window.location.href = '/Product?' + GetParams();
        });
    });

    function GetParams() {
        var category = $("#hCategoryID").val();
        var softField = $("#SoftField :selected").val();
        var softDir = $("#SoftDir :selected").val();
        var status = $("#ddlStatusId :selected").val();
        var viewType = $("#ddlViewType :selected").val();
        var ishot = $("#cbxIsHot").is(":checked");
        var isdiscount = $("#cbxIsDiscount").is(":checked");

        var param = '';
        if (!(category == '' || category == null)) {
            param += '&categoryId=' + category;
        }
        if (!(softField == '' || softField == null)) {
            param += '&softField=' + softField;
        }
        if (!(softDir == '' || softDir == null)) {
            param += '&softDir=' + softDir;
        }
        if (!(status == '' || status == null)) {
            param += '&StatusID=' + status;
        }
        if (!(viewType == '' || viewType == null)) {
            param += '&viewType=' + viewType;
        }
        if (!(ishot == '' || ishot == null)) {
            param += '&IsHot=' + ishot;
        }
        if (!(isdiscount == '' || isdiscount == null)) {
            param += '&IsDiscount=' + isdiscount;
        }
        return param;
    }
</script>
<div>
    @*<h3>@_unitOfWork.CategoryRepository.GetByID(Convert.ToInt32(ViewBag.CurrentCategoryID)).Name</h3>*@
</div>
<div class="adminfilterdiv">
    @Html.Hidden("hCategoryID", categoryId)
    <ul class="productFilter">
        @*<li><span>Category : </span><span>
            @Html.DropDownList("ddlCategory", (SelectList)ViewBag.Categories, string.Empty)
        </span></li>*@
        <li><span>Status : </span><span>
            @Html.DropDownList("ddlStatusId", (SelectList)ViewBag.StatusId, string.Empty)
        </span></li>
        @*<li>
            <span>Configuration : </span>
            <span>
                @Html.DropDownList("Configuration", (SelectList)ViewBag.Configuration)
            </span>
        </li>*@
        <li><span>Sort By : </span><span>
            @Html.DropDownList("SoftField", (SelectList)ViewBag.SoftFields, string.Empty)
        </span></li>
        <li><span>Sort Dir : </span><span>
            @Html.DropDownList("SoftDir", (SelectList)ViewBag.SoftDir, string.Empty)
        </span></li>
        <li><span>View Type : </span><span>
            @Html.DropDownList("ddlViewType", (SelectList)ViewBag.ViewType, string.Empty)
        </span></li>
    </ul>
</div>
<div class="adminLeftFilterDiv">
    <ul class="productLeftFilter">
        <li><span>Is hot : </span><span>@*<input type="checkbox" id="cbxIsHot" value="@ViewBag.CurrentIsHot" />*@
            @Html.CheckBox("cbxIsHot", (bool)@ViewBag.CurrentIsHot)
        </span></li>
        <li><span>Is discount : </span><span>
            @Html.CheckBox("cbxIsDiscount", (bool)@ViewBag.CurrentIsDiscount)
        </span></li>
    </ul>
</div>
<div class="clearboth">
    <span></span>
</div>
<div>
    @{
        var p_index = 1;
    }
    @foreach (var item in Model)
    {

        var link_id = "product_item_" + p_index;
        p_index++;
        <div class="product_item">
            <div>
                <h3>
                    <a class="product_item_link" id="@link_id" href="@item.ID">@item.Name</a></h3>
            </div>
            <table>
                <tr>
                    <td style="vertical-align: top; padding: 5px;">
                        @*<a href="@Url.Action("Details", "Product", new { id = item.ID })">*@
                        @{
                            var cate = _unitOfWork.CategoryRepository.GetByID(Convert.ToInt32(categoryId));
                        }
                        <a href="@Url.Action("Details", "Product", new { id = item.ID,title = item.Name, categoryId = cate.ID, categoryName = cate.Name })">
                            <img width="100px" src="@item.ThumbImage" alt="thumbImage" /></a>
                    </td>
                    <td>
                        <table width="100%">
                            <tr>
                                <td style="width: 150px">
                                    Price
                                </td>
                                <td>
                                    @item.Price
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Discount
                                </td>
                                <td>
                                    @item.DiscountByPercent
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Code
                                </td>
                                <td>
                                    @item.Code
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Warranty
                                </td>
                                <td>
                                    @item.Warranty
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Manufacture
                                </td>
                                <td>
                                    @item.Manufacture
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Status
                                </td>
                                <td>
                                    @item.Status.Name
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    @item.Description
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    }
</div>
<div class="common_paging">
    <ul class="nav_common_ul">
        @{
            if (ViewBag.CurrentPage > 1)
            {
                
            <li><a href="@GetProductLink()" class="common_paging_first">First</a></li>
            <li><a href="@GetProductLink((int)ViewBag.CurrentPage - 1)" class="common_paging_previous">
                Prev</a></li>
            }
            <li>page
                <input id="goToPage" type="text" class="common_pagging_page" style="width: 50px" value="@ViewBag.CurrentPage"/>
                of @ViewBag.NumberOfPage
            </li>
            if (ViewBag.CurrentPage < ViewBag.NumberOfPage)
            {
            <li><a href="@GetProductLink((int)ViewBag.CurrentPage + 1)" class="common_paging_next">
                Next</a></li>
            <li><a href="@GetProductLink((int)ViewBag.NumberOfPage)" class="common_paging_last">
                Last</a></li>
            }
        }
    </ul>
</div>
<div id="ViewDetailAjaxBackground">
</div>
<div id="ViewDetailAjaxWrapper">
    <div id="ViewDetailAjaxMain">
        <div id="WindowTopBar">
            <div class="WindowExitButton">
                <a id="WindowExitButtonLink" href="#">
                    <img src="../../../Content/images/exit.png" alt="exit" />
                </a>
            </div>
            <div class="clearboth">
                <span></span>
            </div>
        </div>
        <div id="WindowBody">
            <div id="WindowBodyLeft">
                <a id="WindowPrevButtonLink" href="#">
                    <div id="WindowPrevButton">
                        <img src="../../../Content/images/previous.png" />
                    </div>
                </a>
            </div>
            <div id="WindowBodyCenter">
            </div>
            <div id="WindowBodyRight">
                <a id="WindowNextButtonLink" href="#">
                    <div id="WindowNextButton">
                        <img src="../../../Content/images/next.png" />
                    </div>
                </a>
            </div>
        </div>
        <div class="clearboth">
            <span></span>
        </div>
        <div id="WindowBottomBar">
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        var curr_p_index = 0;


        $("#ViewDetailAjaxBackground").hide();
        $("#ViewDetailAjaxWrapper").hide();

        $(".product_item_link").click(function (e) {
            e.preventDefault();
            var link_id = e.target.id.split("_");
            curr_p_index = link_id[link_id.length - 1];
            var link_href = this.href.split('/');
            var id = link_href[link_href.length - 1];

            $("#ViewDetailAjaxBackground").show();
            $("#ViewDetailAjaxWrapper").show();

            GetProductDetails(id);
        });

        $("#WindowPrevButtonLink").click(function (e) {
            e.preventDefault();
            if (curr_p_index > 1) {
                curr_p_index--;
            }
            else {
                curr_p_index = 1;
            }
            var link_href = document.getElementById("product_item_" + curr_p_index).href.split('/');
            var id = link_href[link_href.length - 1];

            GetProductDetails(id);

        });


        $("#WindowNextButtonLink").click(function (e) {
            e.preventDefault();
            var lastItemIndex = $(".product_item_link").length;
            if (curr_p_index < lastItemIndex) {
                curr_p_index++;
            }
            else {
                curr_p_index = lastItemIndex;
            }
            var link_href = document.getElementById("product_item_" + curr_p_index).href.split('/');
            var id = link_href[link_href.length - 1];

            GetProductDetails(id);

        });

        $("#WindowExitButtonLink").click(function (e) {
            e.preventDefault();
            $("#ViewDetailAjaxBackground").hide();
            $("#ViewDetailAjaxWrapper").hide();
        });
    });
    function GetProductDetails(id) {
        $.ajax({
            url: '/Product/CompactDetails/' + id,
            type: "GET",
            dataType: 'html',
            //data: '{"productId" : "' + id + '"}',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $("#WindowBodyCenter").html(data);
            }
        });
    }
</script>
